//
//  StoreBriefCell.m
//  WisdomZhuHai
//
//  Created by Admin on 14-10-28.
//  Copyright (c) 2014年 wyd. All rights reserved.
//

#import "StoreBriefCell.h"
#import "Common.h"

const static CGFloat MARGIN_X = 8;
const static CGFloat MARGIN_Y = 8;
const static CGFloat OFFSET_X = 8;
const static CGFloat OFFSET_Y = 6;
const static CGFloat INTERVAL_Y = 4;
const static CGSize TITLE_SIZE = {160, 28};
const static CGSize HOMEPAGE_BUTTON_SIZE = {55, 24};
const static CGFloat CONTENT_FONT_SIZE = 15;
const static CGFloat CONTENT_NORMAL_HEIGHT = 40;
const static CGFloat SHOWALL_BUTTON_HEIGHT = 22;
#define CONTEN_WIDTH ([UIScreen mainScreen].bounds.size.width - MARGIN_X*2 - OFFSET_X*2)

@interface StoreBriefCell()

@end

@implementation StoreBriefCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIView *cntView = [[UIView alloc] initWithFrame:CGRectMake(MARGIN_X, MARGIN_Y, self.contentView.bounds.size.width - MARGIN_X*2, MARGIN_Y*2 + OFFSET_Y*2 + TITLE_SIZE.height)];
        cntView.tag = 1000;
        cntView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:cntView];
        
        UILabel *lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(OFFSET_X, OFFSET_Y, TITLE_SIZE.width, TITLE_SIZE.height)];
        lbTitle.tag = 1001;
        lbTitle.font = [UIFont systemFontOfSize:16];
        lbTitle.backgroundColor = [UIColor clearColor];
        lbTitle.textColor = [UIColor blackColor];
        [cntView addSubview:lbTitle];
        
        UIButton *btnHomePage = [UIButton buttonWithType:UIButtonTypeCustom];
        btnHomePage.tag = 1002;
        btnHomePage.frame = CGRectMake(cntView.bounds.size.width - OFFSET_X - HOMEPAGE_BUTTON_SIZE.width, OFFSET_Y + (TITLE_SIZE.height - HOMEPAGE_BUTTON_SIZE.height)/2, HOMEPAGE_BUTTON_SIZE.width, HOMEPAGE_BUTTON_SIZE.height);
        [btnHomePage setImage:[UIImage imageNamed:@"hotel/official1.png"] forState:UIControlStateNormal];
        [btnHomePage setImage:[UIImage imageNamed:@"hotel/official2.png"] forState:UIControlStateHighlighted];
        [btnHomePage addTarget:self action:@selector(onClickHomePage:) forControlEvents:UIControlEventTouchUpInside];
        [cntView addSubview:btnHomePage];
        
        UILabel *lbContent = [[UILabel alloc] initWithFrame:CGRectMake(OFFSET_X, OFFSET_Y + INTERVAL_Y + HOMEPAGE_BUTTON_SIZE.height, cntView.bounds.size.width - OFFSET_X*2, CONTENT_NORMAL_HEIGHT)];
        lbContent.tag = 1003;
        lbContent.textColor = [UIColor darkGrayColor];
        lbContent.font = [UIFont systemFontOfSize:CONTENT_FONT_SIZE];
        lbContent.numberOfLines = 0;
        [cntView addSubview:lbContent];
        
        UIButton *btnShowAll = [UIButton buttonWithType:UIButtonTypeCustom];
        btnShowAll.tag = 1004;
        btnShowAll.hidden = YES;
        btnShowAll.frame = CGRectMake(OFFSET_X, 0, CONTEN_WIDTH, SHOWALL_BUTTON_HEIGHT);
        [btnShowAll setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        btnShowAll.titleLabel.font = [UIFont systemFontOfSize:15];
        [btnShowAll addTarget:self action:@selector(onClickShowAll:) forControlEvents:UIControlEventTouchUpInside];
        [cntView addSubview:btnShowAll];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)onClickHomePage:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(onClickHomePage:)]) {
        [self.delegate onClickHomePage:self];
    }
}

- (void)onClickShowAll:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(onClickShowAll:)]) {
        [self.delegate onClickShowAll:self];
    }
}

+ (CGFloat)getCellHeight:(NSString*)content ifShowAll:(BOOL)ifShowAll
{
    CGSize size = [Common textSizeWithFont:[UIFont systemFontOfSize:CONTENT_FONT_SIZE] constrainedToSize:CGSizeMake(CONTEN_WIDTH, MAXFLOAT) string:content];
    CGFloat height = MARGIN_Y*2 + OFFSET_Y*2 + INTERVAL_Y + TITLE_SIZE.height;
    if (size.height <= CONTENT_NORMAL_HEIGHT) {
        return height + size.height;
    }
    if (ifShowAll) {
        return height + size.height + INTERVAL_Y + SHOWALL_BUTTON_HEIGHT;
    }
    else {
        return height + CONTENT_NORMAL_HEIGHT + INTERVAL_Y + SHOWALL_BUTTON_HEIGHT;
    }
}

- (void)setTitle:(NSString*)title
{
    UILabel *lb = (UILabel*)[self viewWithTag:1001];
    lb.text = title;
}

- (void)setContent:(NSString*)content ifShowAll:(BOOL)ifShowAll
{
    UIView *cntView = [self viewWithTag:1000];
    UILabel *lbContent = (UILabel*)[self viewWithTag:1003];
    UIButton *btnShowAll = (UIButton*)[self viewWithTag:1004];
    
    lbContent.text = content;
    
    CGSize size = [Common textSizeWithFont:[UIFont systemFontOfSize:CONTENT_FONT_SIZE] constrainedToSize:CGSizeMake(CONTEN_WIDTH, MAXFLOAT) string:content];
    CGFloat height = OFFSET_Y + TITLE_SIZE.height + INTERVAL_Y;
    if (size.height <= CONTENT_NORMAL_HEIGHT) {
        btnShowAll.hidden = YES;
        
        CGRect frame = lbContent.frame;
        frame.size.height = size.height+1;
        lbContent.frame = frame;
        
        height += size.height;
    }
    else {
        btnShowAll.hidden = NO;
        if (ifShowAll) {
            [btnShowAll setTitle:@"收起" forState:UIControlStateNormal];
        }
        else {
            [btnShowAll setTitle:@"更多" forState:UIControlStateNormal];
            size.height = CONTENT_NORMAL_HEIGHT;
        }
        
        CGRect frame = lbContent.frame;
        frame.size.height = size.height + 1;
        lbContent.frame = frame;
        
        height += size.height + INTERVAL_Y;
        frame = btnShowAll.frame;
        frame.origin.y = height;
        btnShowAll.frame = frame;
        
        height += SHOWALL_BUTTON_HEIGHT;
    }
    
    height += OFFSET_Y;
    CGRect frame = cntView.frame;
    frame.size.height = height;
    cntView.frame = frame;
}

@end
